package com.parse.ip.parseip.controller;

import com.parse.ip.parseip.entity.Result;
import com.parse.ip.parseip.entity.Telephone;
import com.parse.ip.parseip.factory.PareFactory;
import com.parse.ip.parseip.service.ParseIpService;
import com.parse.ip.parseip.service.ParseTelephone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/parse")
public class ParseIpController {

    public static Logger logger = LoggerFactory.getLogger(ParseIpController.class);

    @Autowired
    private ParseIpService parseIpService;

    @Autowired
    private ParseTelephone parseTelephone;

    /**
     * 主动解析IP获取归属地
     * @param request
     * @return
     */
    @RequestMapping(value = "/ip",method = RequestMethod.GET)
    public Result pareIp(HttpServletRequest request) {
        Result result = new Result();
        try {
            String ip = getIpAddr(request);//获取连接ip
            //{"27.195.157.210":{"iP":"27.195.157.210","network":"山东省淄博市 联通"}}
            String network = PareFactory.parse().parseIp(ip);//解析ip归属地,,获取到一个json字符串
            parseIpService.save(network);
            Object object = parseIpService.getNetwork(ip);
            result.setPlace(object);
            result.setMessage("ip地址解析成功");
            result.setSuccess(true);
            logger.info("获取到的ip是--->"+ip);
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("ip地址解析失败,失败原因:"+e);
            result.setErrorMessage("ip地址查询失败");
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 用户传入ip解析归属地
     * @param ip
     * @return
     */
    @RequestMapping(value = "/ip/{ip}",method = RequestMethod.GET)
    public Result pareIp(@PathVariable(value = "ip") String ip){
        Result result = new Result();
        try {
            String network = PareFactory.parse().parseIp(ip);
            //String network = ParseIP.parseUrl(ip);
            parseIpService.save(network);
            Object object = parseIpService.getNetwork(ip);
            result.setPlace(object);
            result.setSuccess(true);
            result.setMessage("ip地址解析成功-->"+ip);
            logger.info("被解析的ip是-->"+ip);
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("ip地址解析失败,失败原因:"+e);
            result.setErrorMessage("ip地址查询失败"+ip);
            result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping(value = "/telephone/{tel}",method = RequestMethod.GET)
    public Result parseTel(@PathVariable(value = "tel") String tel){
        Result result = new Result();
        try {
            Telephone telephone = PareFactory.parse().paseTelephone(tel);
            parseTelephone.save(telephone);
            result.setSuccess(true);
            result.setMessage("手机号解析成功");
            result.setTelephone(telephone.getTelephone());
            result.setLocation(telephone.getCarrier());
            logger.info("被解析的电话是-->"+tel);
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("电话地址解析失败,失败原因:"+e);
        }
        return result;
    }

    /**
     * 获取ip地址
     * @param request
     * @return
     */
    private String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
